
TARGET = Target

CFLAGS := -mcmodel=large -fno-builtin -m64 -fno-stack-protector

ASFLAGS := --64

PIC := APIC

all: $(TARGET)/systemTmp $(TARGET)/Kallsyms.o
	ld -b elf64-x86-64 -z muldefs -o $(TARGET)/system $(TARGET)/head.o $(TARGET)/entry.o $(TARGET)/Main.o $(TARGET)/Printf.o $(TARGET)/FontASCII.o $(TARGET)/Lib.o $(TARGET)/Gate.o $(TARGET)/Trap.o $(TARGET)/Memory.o $(TARGET)/Slab.o $(TARGET)/Malloc.o $(TARGET)/Interrupt.o $(TARGET)/Task.o $(TARGET)/switch.o $(TARGET)/SystemService.o $(TARGET)/Cpu.o $(TARGET)/PIC.o $(TARGET)/SMP.o $(TARGET)/APUBoot.o $(TARGET)/Schedule.o $(TARGET)/HEPT.o $(TARGET)/SoftIRQ.o $(TARGET)/Time.o $(TARGET)/Timer.o $(TARGET)/Atomic.o $(TARGET)/Semaphore.o $(TARGET)/Spinlock.o $(TARGET)/Kallsyms.o -T Kernel.lds
	objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary $(TARGET)/system $(TARGET)/kernel.bin

$(TARGET)/systemTmp:	$(TARGET)/head.o $(TARGET)/entry.o $(TARGET)/Main.o $(TARGET)/Printf.o $(TARGET)/FontASCII.o $(TARGET)/Lib.o $(TARGET)/Gate.o $(TARGET)/Trap.o $(TARGET)/Memory.o $(TARGET)/Slab.o $(TARGET)/Malloc.o $(TARGET)/Interrupt.o $(TARGET)/Task.o $(TARGET)/switch.o $(TARGET)/SystemService.o $(TARGET)/Cpu.o $(TARGET)/PIC.o $(TARGET)/SMP.o $(TARGET)/APUBoot.o $(TARGET)/Schedule.o $(TARGET)/HEPT.o $(TARGET)/SoftIRQ.o $(TARGET)/Time.o $(TARGET)/Timer.o $(TARGET)/Atomic.o $(TARGET)/Semaphore.o $(TARGET)/Spinlock.o
	ld -b elf64-x86-64 -z muldefs -o $(TARGET)/systemTmp $(TARGET)/head.o $(TARGET)/entry.o $(TARGET)/Main.o $(TARGET)/Printf.o $(TARGET)/FontASCII.o $(TARGET)/Lib.o $(TARGET)/Gate.o $(TARGET)/Trap.o $(TARGET)/Memory.o $(TARGET)/Slab.o $(TARGET)/Malloc.o $(TARGET)/Interrupt.o $(TARGET)/Task.o $(TARGET)/switch.o $(TARGET)/SystemService.o $(TARGET)/Cpu.o $(TARGET)/PIC.o $(TARGET)/SMP.o $(TARGET)/APUBoot.o $(TARGET)/Schedule.o $(TARGET)/HEPT.o $(TARGET)/SoftIRQ.o $(TARGET)/Time.o $(TARGET)/Timer.o $(TARGET)/Atomic.o $(TARGET)/Semaphore.o $(TARGET)/Spinlock.o -T Kernel.lds

$(TARGET)/head.o:	head.S
	gcc -E  head.S > $(TARGET)/head.s
	as $(ASFLAGS) -o $(TARGET)/head.o $(TARGET)/head.s

$(TARGET)/entry.o: ExceptionManagement/entry.S
	gcc -E  ExceptionManagement/entry.S > $(TARGET)/entry.s
	as $(ASFLAGS) -o $(TARGET)/entry.o $(TARGET)/entry.s

$(TARGET)/Main.o:	Main.c
	gcc  $(CFLAGS) -c Main.c -D$(PIC) -o $(TARGET)/Main.o

$(TARGET)/Printf.o: GeneralLibrary/Printf.c
	gcc  $(CFLAGS) -c GeneralLibrary/Printf.c -o $(TARGET)/Printf.o

$(TARGET)/FontASCII.o: Font/FontASCII.c
	gcc  $(CFLAGS) -c Font/FontASCII.c -o $(TARGET)/FontASCII.o

$(TARGET)/Lib.o: GeneralLibrary/Lib.c
	gcc  $(CFLAGS) -c GeneralLibrary/Lib.c -o $(TARGET)/Lib.o

$(TARGET)/Gate.o: ExceptionManagement/Gate.c
	gcc  $(CFLAGS) -c ExceptionManagement/Gate.c -o $(TARGET)/Gate.o

$(TARGET)/Trap.o: ExceptionManagement/Trap.c
	gcc  $(CFLAGS) -c ExceptionManagement/Trap.c -o $(TARGET)/Trap.o

$(TARGET)/Memory.o: MemoryManagement/Memory.c
	gcc  $(CFLAGS) -c MemoryManagement/Memory.c -o $(TARGET)/Memory.o

$(TARGET)/Slab.o: MemoryManagement/Slab.c
	gcc  $(CFLAGS) -c MemoryManagement/Slab.c -o $(TARGET)/Slab.o

$(TARGET)/Malloc.o: MemoryManagement/Malloc.c
	gcc  $(CFLAGS) -c MemoryManagement/Malloc.c -o $(TARGET)/Malloc.o

$(TARGET)/Interrupt.o: InterruptManagement/Interrupt.c
	gcc  $(CFLAGS) -c InterruptManagement/Interrupt.c -o $(TARGET)/Interrupt.o

$(TARGET)/Task.o: ProcessManagement/Task.c
	gcc  $(CFLAGS) -c ProcessManagement/Task.c -o $(TARGET)/Task.o

$(TARGET)/Cpu.o: ProcessManagement/Cpu.c
	gcc  $(CFLAGS) -c ProcessManagement/Cpu.c -o $(TARGET)/Cpu.o

$(TARGET)/switch.o: ProcessManagement/switch.S
	gcc -E  ProcessManagement/switch.S > $(TARGET)/switch.s
	as $(ASFLAGS) -o $(TARGET)/switch.o $(TARGET)/switch.s

$(TARGET)/SystemService.o: SystemServiceManagement/SystemService.c
	gcc  $(CFLAGS) -c SystemServiceManagement/SystemService.c -o $(TARGET)/SystemService.o

ifeq ($(PIC),APIC)
$(TARGET)/PIC.o: InterruptManagement/APIC.c
	gcc  $(CFLAGS) -c InterruptManagement/APIC.c -o $(TARGET)/PIC.o
else
$(TARGET)/PIC.o: InterruptManagement/8259A.c
	gcc  $(CFLAGS) -c InterruptManagement/8259A.c -o $(TARGET)/PIC.o
endif

$(TARGET)/SMP.o: ProcessManagement/SMP.c
	gcc  $(CFLAGS) -c ProcessManagement/SMP.c -o $(TARGET)/SMP.o

$(TARGET)/APUBoot.o:	ProcessManagement/APUBoot.S
	gcc -E  ProcessManagement/APUBoot.S > $(TARGET)/APUBoot.s
	as $(ASFLAGS) -o $(TARGET)/APUBoot.o $(TARGET)/APUBoot.s

$(TARGET)/Schedule.o: ProcessManagement/Schedule.c
	gcc  $(CFLAGS) -c ProcessManagement/Schedule.c -o $(TARGET)/Schedule.o

$(TARGET)/HEPT.o: ProcessManagement/KernelTimer/HEPT.c
	gcc  $(CFLAGS) -c ProcessManagement/KernelTimer/HEPT.c -o $(TARGET)/HEPT.o

$(TARGET)/SoftIRQ.o: ProcessManagement/SoftIRQ.c
	gcc  $(CFLAGS) -c ProcessManagement/SoftIRQ.c -o $(TARGET)/SoftIRQ.o

$(TARGET)/Time.o: ProcessManagement/KernelTimer/Time.c
	gcc  $(CFLAGS) -c ProcessManagement/KernelTimer/Time.c -o $(TARGET)/Time.o

$(TARGET)/Timer.o: ProcessManagement/KernelTimer/Timer.c
	gcc  $(CFLAGS) -c ProcessManagement/KernelTimer/Timer.c -o $(TARGET)/Timer.o

$(TARGET)/Atomic.o: ProcessManagement/ProcessSync/Atomic.c
	gcc  $(CFLAGS) -c ProcessManagement/ProcessSync/Atomic.c -o $(TARGET)/Atomic.o

$(TARGET)/Semaphore.o: ProcessManagement/ProcessSync/Semaphore.c
	gcc  $(CFLAGS) -c ProcessManagement/ProcessSync/Semaphore.c -o $(TARGET)/Semaphore.o

$(TARGET)/Spinlock.o: ProcessManagement/ProcessSync/Spinlock.c
	gcc  $(CFLAGS) -c ProcessManagement/ProcessSync/Spinlock.c -o $(TARGET)/Spinlock.o

$(TARGET)/Kallsyms.o: Debug/Kallsyms.c $(TARGET)/systemTmp
	gcc -o $(TARGET)/Kallsyms Debug/Kallsyms.c
#	nm -n $(TARGET)/systemTmp | $(TARGET)/./Kallsyms > $(TARGET)/Kallsyms.S
	gcc -c $(TARGET)/Kallsyms.S -o $(TARGET)/Kallsyms.o

clean:
	rm -rf *.o *.s~ *.s *.S~ *.c~ *.h~ system systemTmp Makefile~ Kernel.lds~ kernel.bin Kallsyms Kallsyms.o Kallsyms.S
